﻿<?php
//系统全局可使用函数 公共函数

//获取当前毫秒时间戳		2016/5/3		
function msec()
{
	list($usec,$sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

//动态获取相对页面 $opt R/r 返回廋URL; array 重置url - ?? bug/	分页情况下重复生成文件
function url($opt=null,$append=false)
{
	$url = $_SERVER["REQUEST_URI"];//胖URL
	$param = $_SERVER["QUERY_STRING"];//URL参数
	$ori = str_replace('?'.$param,'',$url);//廋URL

	if(in_array($opt,array('R','r')) && !is_array($opt)){//重置
		$url = str_replace('?'.$param,'',$url);
	}elseif(in_array($opt,array('G','r')) && !is_array($opt)){//返回 url
		$url = $_SERVER["REQUEST_SCHEME"].'://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
	}elseif(is_array($opt)){//数组重置
		$arr = array();
		if($append){//附加变量的方式@2016年7月6日
			$t1 = str_replace('=','":"',$param);
			$t1 = '{"'.str_replace('&','","',$t1).'"}';
			if(is_array(json_decode($t1,true))) $opt = array_merge(json_decode($t1,true),$opt);
		}
		foreach ($opt as $k=>$v) {
			$arr[] = $k.'='.$v;
		}
		$str = implode('&',$arr);
		$url = $ori.'?'.$str;
	}
	return $url;
}
//	url 改进方法
function resetUrl($opt=null)
{
	$url = $_SERVER["REQUEST_URI"];
	$tmpArr = explode('?',$url);
	$slamUrl = $tmpArr[0];// 瘦url
	$bind = $_GET;
	if(is_array($opt)){//	重置URL
		$opt = array_merge($bind,$opt);
		$tmpArr = array();
		foreach($opt as $k=>$v){
			$tmpArr[] = $k.'='.$v;
		}
		$slamUrl .= '?'.implode('&',$tmpArr);
		return $slamUrl;
	}
	return $slamUrl;
}

//返回系统ip
function getIp()
{
	if (!empty($_SERVER['HTTP_CLIENT_IP']))
	{
		$ip=$_SERVER['HTTP_CLIENT_IP'];
	}
	elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
	//检查IP是从代理传递
	{
		$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
	}
	else
	{
		$ip=$_SERVER['REMOTE_ADDR'];
	}
	return $ip;
}
/************************系统扩展***************************/
//	bug PHP不同版本反应不一致
function app_name($type=null)
{
    $url = $_SERVER["REDIRECT_URL"];// TeClan/public/Clan/crtgen-PHP5.4.
	if(substr_count($url,'http://')>0){
		$url = $_SERVER["REQUEST_URI"];//	PHP5.6.15
		$arr = explode('/',$url);
		$app = empty($arr[2])? 'Index':$arr[2];
		if(isset($arr[3])){//  /TeClan/Node/index?genno=1
			$tmp = explode('?',$arr[3]);
			$arr[3] = $tmp[0];
		}
		$action = empty($arr[3])? 'Index':$arr[3];		
	}else{
		$arr = explode('/',$url);		
    	$app = empty($arr[3])? 'Index':$arr[3];
    	$action = empty($arr[4])? 'index':$arr[4];
	}    
    $tmp = ['app'=>$app,'action'=>$action];	
	//debug($tmp);
    if(empty($type)) return $tmp;
    if(array_key_exists($type,$tmp)) return $tmp[$type];
    return '';
}
//公共头部 2016年7月18日
function pblichead($tpl=null)
{
	$xhtml = '
		<link rel="icon" href="/TeClan/public/img/teclan.ico" type="image/x-icon" />
		<link rel="shortcut icon" href="/TeClan/public/img/teclan.ico" type="image/x-icon" />
		<link rel="stylesheet" type="text/css" href="/TeClan/public/img/teclan.icocro_common.css"/>
		<meta name="Author" content="Joshua Conero Doeeking">
		<meta name="Keywords" content="Conero 集团">
		<meta name="Description" content="个人网站项目设计">
	
		<script src="/TeClan/public/js/jquery-1.12.4.min.js"></script>
		<script src="/TeClan/public/js/teclan.js"></script>
		<link href="/TeClan/public/css/TeClan.css" type="text/css" rel="stylesheet">
	';
	if($tpl) $xhtml .= $tpl;
	return $xhtml;
}

//debug 数据输出 2016年7月18日
function debug($data)
{
	$logfile = APP_PATH.'log/'.date('Y-m-d').'.log';
	$fh = fopen($logfile,'a+');
	$data = is_array($data)? print_r($data,true):$data;
	$data = "\r\n~~~~~~~~~~~~~~~~~~~(".date('Y-m-d H:i:s')."),系统调试输出：\r\n".$data;
	fwrite($fh,$data);
	fclose($fh);
}
//Ajax数据解密-
function ajax()
{
	$data = $_POST;
	if(!isset($data['ajax']) || $data['ajax'] != 'Y') return array(); 
	if(isset($data['uid'])) unset($data['uid']);
	unset($data['ajax']);
	foreach($data as $k=>$v)
	{
		$tmp = explode('$$',base64_decode($v));
		$data[$k] = $tmp[0];
	}
	return $data;
}
//Ajax数据返回
function ajaxReturn($data)
{
	// 返回JSON数据格式到客户端 包含状态信息
	header('Content-Type:application/json; charset=utf-8');
	exit(json_encode($data));
}
//动态生成dialog框
function dialog($tool=null)
{
	$xhtml = '
		<div id="page_dialog" style="display:none;" draggable="true">
			<div class="dislog_bar">
				<p id="pdialog_title"></p>
				<p id="pdialog_helper">'.($tool? $tool:'').'<a href="JavaScript:Te.dialog(\'close\');">关闭</a><a href="JavaScript:Te.dialog_full();" isfull="N" bind="" id="dialog_full_btn">全屏</a></p>
			</div>			
			<div id="dialog_ctt">
			</div>
		</div>
	';
	return $xhtml;
}
?>